<template>
  <div class="notifications-container">
    <!-- 头部 -->
    <div class="notifications-header">
      <el-button 
        icon="ArrowLeft" 
        circle 
        @click="goBack"
        class="back-btn"
      />
      <h2 class="header-title">消息通知</h2>
      <el-button 
        icon="Check" 
        circle 
        @click="markAllRead"
        v-if="unreadCount > 0"
      />
    </div>

    <!-- 通知列表 -->
    <div class="notifications-list">
      <div 
        v-for="notification in notifications" 
        :key="notification.id"
        :class="['notification-item', { unread: !notification.isRead }]"
        @click="markAsRead(notification)"
      >
        <div class="notification-icon">
          <el-icon :size="24" :color="getNotificationColor(notification.type)">
            <component :is="getNotificationIcon(notification.type)" />
          </el-icon>
        </div>
        <div class="notification-content">
          <h4 class="notification-title">{{ notification.title }}</h4>
          <p class="notification-message">{{ notification.content }}</p>
          <div class="notification-meta">
            <span class="notification-time">{{ formatTime(notification.createdAt) }}</span>
            <el-tag 
              :type="getNotificationTagType(notification.type)"
              size="small"
            >
              {{ getNotificationTypeText(notification.type) }}
            </el-tag>
          </div>
        </div>
        <div class="notification-actions">
          <el-button 
            v-if="!notification.isRead"
            type="text" 
            size="small"
            @click.stop="markAsRead(notification)"
          >
            标记已读
          </el-button>
        </div>
      </div>
      
      <!-- 空状态 -->
      <div v-if="notifications.length === 0" class="empty-state">
        <el-icon size="60" color="#ccc"><Bell /></el-icon>
        <p>暂无通知</p>
      </div>
    </div>

    <!-- 加载更多 -->
    <div v-if="hasMore" class="load-more">
      <el-button 
        :loading="loading"
        @click="loadMore"
      >
        加载更多
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getUserNotifications, markNotificationRead, markAllNotificationsRead } from '@/api/notification'
import type { Notification } from '@/types'
import dayjs from '@/utils/dayjs'

// 响应式数据
const notifications = ref<Notification[]>([])
const loading = ref(false)
const hasMore = ref(true)
const currentPage = ref(1)
const unreadCount = ref(0)

// 获取路由
const router = useRouter()

// 返回上一页
const goBack = () => {
  router.back()
}

// 标记为已读
const markAsRead = async (notification: Notification) => {
  if (notification.isRead) return
  
  try {
    await markNotificationRead(notification.id)
    notification.isRead = true
    unreadCount.value = Math.max(0, unreadCount.value - 1)
    ElMessage.success('已标记为已读')
  } catch (error) {
    console.error('标记已读失败:', error)
    ElMessage.error('标记已读失败')
  }
}

// 标记全部已读
const markAllRead = async () => {
  try {
    await markAllNotificationsRead()
    notifications.value.forEach(notification => {
      notification.isRead = true
    })
    unreadCount.value = 0
    ElMessage.success('已全部标记为已读')
  } catch (error) {
    console.error('标记全部已读失败:', error)
    ElMessage.error('标记全部已读失败')
  }
}

// 获取通知图标
const getNotificationIcon = (type: string) => {
  const iconMap: Record<string, string> = {
    'PERFORMANCE': 'VideoPlay',
    'VOTE': 'Star',
    'SYSTEM': 'Setting',
    'PROMOTION': 'Gift',
    'REMINDER': 'Bell'
  }
  return iconMap[type] || 'Bell'
}

// 获取通知颜色
const getNotificationColor = (type: string) => {
  const colorMap: Record<string, string> = {
    'PERFORMANCE': '#667eea',
    'VOTE': '#ff6b6b',
    'SYSTEM': '#4ecdc4',
    'PROMOTION': '#ffa726',
    'REMINDER': '#45b7d1'
  }
  return colorMap[type] || '#666'
}

// 获取通知标签类型
const getNotificationTagType = (type: string) => {
  const tagMap: Record<string, string> = {
    'PERFORMANCE': 'primary',
    'VOTE': 'success',
    'SYSTEM': 'info',
    'PROMOTION': 'warning',
    'REMINDER': 'danger'
  }
  return tagMap[type] || 'info'
}

// 获取通知类型文本
const getNotificationTypeText = (type: string) => {
  const textMap: Record<string, string> = {
    'PERFORMANCE': '演出',
    'VOTE': '投票',
    'SYSTEM': '系统',
    'PROMOTION': '活动',
    'REMINDER': '提醒'
  }
  return textMap[type] || '通知'
}

// 格式化时间
const formatTime = (time: string) => {
  const now = dayjs()
  const notificationTime = dayjs(time)
  const diffMinutes = now.diff(notificationTime, 'minute')
  
  if (diffMinutes < 1) {
    return '刚刚'
  } else if (diffMinutes < 60) {
    return `${diffMinutes}分钟前`
  } else if (diffMinutes < 1440) {
    return `${Math.floor(diffMinutes / 60)}小时前`
  } else {
    return notificationTime.format('MM-DD HH:mm')
  }
}

// 获取通知列表
const fetchNotifications = async (page = 1) => {
  try {
    loading.value = true
    const response = await getUserNotifications({ 
      page, 
      limit: 20 
    })
    
    if (page === 1) {
      notifications.value = response.data.notifications
    } else {
      notifications.value.push(...response.data.notifications)
    }
    
    // 计算未读数量
    unreadCount.value = notifications.value.filter(n => !n.isRead).length
    
    hasMore.value = response.data.total > notifications.value.length
    currentPage.value = page
  } catch (error) {
    console.error('获取通知列表失败:', error)
    ElMessage.error('获取通知列表失败')
  } finally {
    loading.value = false
  }
}

// 加载更多
const loadMore = () => {
  if (!loading.value && hasMore.value) {
    fetchNotifications(currentPage.value + 1)
  }
}

// 页面挂载时执行
onMounted(() => {
  fetchNotifications()
})
</script>

<style lang="scss" scoped>
.notifications-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 20px;
}

.notifications-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.back-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.notifications-list {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.notification-item {
  background: white;
  padding: 20px;
  border-radius: 15px;
  display: flex;
  align-items: flex-start;
  gap: 15px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
  }
  
  &.unread {
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
    border-left: 4px solid #667eea;
  }
}

.notification-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: rgba(102, 126, 234, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.notification-content {
  flex: 1;
  
  .notification-title {
    font-size: 16px;
    font-weight: 600;
    color: #333;
    margin: 0 0 8px 0;
  }
  
  .notification-message {
    font-size: 14px;
    color: #666;
    margin: 0 0 10px 0;
    line-height: 1.4;
  }
  
  .notification-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .notification-time {
      font-size: 12px;
      color: #999;
    }
  }
}

.notification-actions {
  flex-shrink: 0;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #999;
  
  p {
    margin: 15px 0 0 0;
    font-size: 16px;
  }
}

.load-more {
  text-align: center;
  padding: 20px;
}

// 移动端适配
@media screen and (max-width: 768px) {
  .notifications-header {
    padding: 12px 15px;
  }
  
  .header-title {
    font-size: 16px;
  }
  
  .notifications-list {
    padding: 15px;
    gap: 12px;
  }
  
  .notification-item {
    padding: 15px;
  }
  
  .notification-icon {
    width: 40px;
    height: 40px;
  }
  
  .notification-content {
    .notification-title {
      font-size: 14px;
    }
    
    .notification-message {
      font-size: 12px;
    }
    
    .notification-meta {
      flex-direction: column;
      align-items: flex-start;
      gap: 5px;
    }
  }
}
</style> 